<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="index.html">&lt; 14. BSP toolkit</a><br />
<a href="bsp_data.html">&lt; 14.4 Reading information from the tree</a>
</td><td width="60%">
<p align="center">
=====================================<br />
14.5 Traversing the tree<br />
=====================================<br />
</p></td><td width="20%">
<a href="bsp_delete.html">&gt; 14.6 Destroying a tree</a>
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
</style>
You can scan all the nodes of the tree and have a custom function called back for each node.<br />
Each traversal function returns false if the traversal has been interrupted (a callback returned false).<br />
<p class="code"><pre>
C++ : class ITCODBspCallback {
      public :
          virtual bool visitNode(TCODBsp *node, void *userData) = 0;
      };

      bool TCODBsp::traversePreOrder(ITCODBspCallback *callback, void *userData)
      bool TCODBsp::traverseInOrder(ITCODBspCallback *callback, void *userData)
      bool TCODBsp::traversePostOrder(ITCODBspCallback *callback, void *userData)
      bool TCODBsp::traverseLevelOrder(ITCODBspCallback *callback, void *userData)
      bool TCODBsp::traverseInvertedLevelOrder(ITCODBspCallback *callback, void *userData)
C   : typedef bool (*TCOD_bsp_callback_t)(TCOD_bsp_t *node, void *userData)

      bool TCOD_bsp_traverse_pre_order(TCOD_bsp_t *node, TCOD_bsp_callback_t callback, void *userData)
      bool TCOD_bsp_traverse_in_order(TCOD_bsp_t *node, TCOD_bsp_callback_t callback, void *userData)
      bool TCOD_bsp_traverse_post_order(TCOD_bsp_t *node, TCOD_bsp_callback_t callback, void *userData)
      bool TCOD_bsp_traverse_level_order(TCOD_bsp_t *node, TCOD_bsp_callback_t callback, void *userData)
      bool TCOD_bsp_traverse_inverted_level_order(TCOD_bsp_t *node, TCOD_bsp_callback_t callback, void *userData)
Py  : def bsp_callback(node, userData) : # ...

      bsp_traverse_pre_order(node, callback, userData=0)
      bsp_traverse_in_order(node, callback, userData=0)
      bsp_traverse_post_order(node, callback, userData=0)
      bsp_traverse_level_order(node, callback, userData=0)
      bsp_traverse_inverted_level_order(node, callback, userData=0)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>node</td><td>In the C version, the node reference (generally, the root node).</td></tr>
<tr><td>callback</td><td>The function to call for each node.<br />It receives the current node and the custom data as parameters<br />If it returns false, the traversal is interrupted.</td></tr>
<tr><td>userData</td><td>Custom data to pass to the callback.</td></tr>
</table>
<br />
Pre-order : the callback is called for the current node, then for the left son, then for the right son.<br />
In-order : the callback is called for the left son, then for current node, then for the right son.<br />
Post-order : the callback is called for the left son, then for the right son, then for the current node.<br />
Level-order : the callback is called for the nodes level by level, from left to right.<br />
Inverted level-order : the callback is called in the exact inverse order as Level-order.<br />
<table class="param">
<tr><th>Pre order</th><th>In order</th><th>Post order</th><th>Level order</th><th>Inverted level<br />order</th></tr>
<tr><td><img src="bsp_preorder.png"></td><td><img src="bsp_inorder.png"></td><td><img src="bsp_postorder.png"></td><td><img src="bsp_levelorder.png"></td><td><img src="bsp_invlevelorder.png"></td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : class MyCallback : public ITCODBspCallback {
        public :
          bool visitNode(TCODBsp *node, void *userData) {
            printf("node pos %dx%d size %dx%d level %d\n",node->x,node->y,node->w,node->h,node->level);
            return true;
          }
      };
      myBSP->traversePostOrder(new MyListener(),NULL);
C   : bool my_callback(TCOD_bsp_t *node, void *userData) {
            printf("node pos %dx%d size %dx%d level %d\n",node->x,node->y,node->w,node->h,node->level);
            return true;
      }
      TCOD_bsp_traverse_post_order(my_bsp,my_callback,NULL);
Py  : def my_callback(node, userData) :
            print "node pos %dx%d size %dx%d level %d"%(node.x,node.y,node.w,node.h,node.level))
            return True
      libtcod.bsp_traverse_post_order(my_bsp,my_callback)
</pre></p>

